package it.trekker.dao;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
public class Poi implements Serializable {

	protected long id;
	protected GPSCoordinate location;
	protected Excursion excursion;
	protected String friendlyName;
	
	
	public Poi() {
		this(null,  null);
	}
	
	public Poi(GPSCoordinate location) {
		this(location, null);
	}
	
	public Poi(GPSCoordinate location, String friendlyName) {
		this.location = location;
		this.friendlyName = friendlyName;
		this.id = -1;
	}
	
	public Poi(Poi poi) {
		if(poi != null) {
			this.id = -1;
			this.location = new GPSCoordinate(poi.getLocation());
			this.friendlyName = poi.getFriendlyName();
		}
	}
	
	@Id
	@GeneratedValue
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public GPSCoordinate getLocation() {
		return location;
	}
	
	public void setLocation(GPSCoordinate location) {
		this.location = location;
	}
	
	@ManyToOne(
			fetch = FetchType.LAZY,
			cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.REFRESH}
	)
	public Excursion getExcursion() {
		return excursion;
	}

	public void setExcursion(Excursion excursion) {
		this.excursion = excursion;
	}

	public String getFriendlyName() {
		return friendlyName;
	}
	
	public void setFriendlyName(String friendlyName) {
		this.friendlyName = friendlyName;
	}
	
	@Override
	public String toString() {
		return String.format("POI::%s (lat: %f, long: %f, alt: %f)", friendlyName,
				location.getLatitude(), location.getLatitude(), location.getAltitude());
	}
	
	@Override
	public boolean equals(Object o) {
		if(o == null)
			return false;
		if(this == o)
			return true;
		if(!(o instanceof Poi))
			return false;
		
		Poi otherPoi = (Poi)o;
		
		if(!this.friendlyName.equals(otherPoi.friendlyName))
			return false;
		if(!this.location.equals(otherPoi.location))
			return false;
		
		return true;
	}
	
}
